<template>
  <el-dialog
    :title="cateId ? '编辑分类' : '添加分类'"
    :visible="addDialog"
    width="50%"
    @close="addCateClose"
  >
    <!-- 添加分类的表单 -->
    <el-form
      :model="addCateForm"
      :rules="addCateFormRules"
      ref="addCateFormRef"
      label-width="100px"
    >
      <el-form-item label="分类名称" prop="cat_name">
        <el-input v-model="addCateForm.cat_name"></el-input>
      </el-form-item>
      <el-form-item label="父级分类" prop="cat_pid">
        <el-cascader
          v-model="selectedKeys"
          :options="parentCateList"
          :props="cascaderProps"
          @change="parentCateChange"
          clearable
          change-on-select
        ></el-cascader>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="addCateClose">取 消</el-button>
      <el-button type="primary" @click="confirm">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {
  getNewCateList,
  getParentCateList,
  getCategoryDetail,
  editCate
} from '@/api/cate'
export default {
  props: {
    addDialog: {
      type: Boolean,
      default: false
    },
    addCateForm: {
      type: Object,
      default() {
        return {
          addCateForm: {
            cat_name: '',
            // 添加分类的父级id，0则表示父级为0.添加一级分类
            cat_pid: 0,
            // 添加分类的等级，0则表示添加一级分类
            cat_level: 0
          }
        }
      }
    }
  },
  data() {
    return {
      cateId: null,
      // 校验规则
      addCateFormRules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      // 选中父级 id 数组
      selectedKeys: [],
      // 父级分类的列表
      parentCateList: [],
      // 指定数据显示的规则
      cascaderProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
        expandTrigger: 'hover'
      }
    }
  },
  methods: {
    async confirm() {
      if (!this.cateId) {
        const params = {
          cat_pid:
            this.selectedKeys.length === 0 ? 0 : this.selectedKeys[this.selectedKeys.length - 1],
          cat_name: this.addCateForm.cat_name,
          cat_level: this.selectedKeys.length
        }
        const res = await getNewCateList(params)
        if (res.meta.status === 201) {
          this.$message.success('添加分类成功')
        }
      } else {
        const params = {
          id: this.cateId,
          catName: this.addCateForm.cat_name
        }
        const res = await editCate(params)
        if (res.meta.status === 200) {
          this.$message.success('编辑分类成功')
        }
      }
      this.$emit('update:addDialog', false)
      this.$emit('changeList')
    },
    async getCategoryDetail(id) {
      this.cateId = id
      const res = await getCategoryDetail(id)
      this.addCateForm.cat_name = res.data.cat_name
    },
    async addBtn() {
      const res = await getParentCateList(2)
      this.parentCateList = res.data
    },
    // 级联变化函数
    parentCateChange(a) {
      console.log(a)
    },
    // 关闭对话框 清空表单数据
    addCateClose() {
      this.$emit('update:addDialog', false)
      this.$refs.addCateFormRef.resetFields()
      this.addCateForm.cat_level = 0
      this.addCateForm.cat_pid = 0
      this.selectedKeys = []
    }
  },
  mounted() {
    this.addBtn()
  }
}
</script>

<style lang="less" scoped>
.treeTable {
  margin-top: 15px;
}
.el-cascader {
  width: 100%;
}
</style>
